package com.projectframe.base.common;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.support.ResourceBundleMessageSource;

public class BaseResourceBundleMessageSource extends ResourceBundleMessageSource
{
    private String[] resourceDirectory;

    public String[] getResourceDirectory()
    {
        return resourceDirectory; 
    }
    public void setResourceDirectory(String... resourceDirectory)
    {
        this.resourceDirectory = resourceDirectory;
        
        if(resourceDirectory==null || resourceDirectory.length==0)
            return;
        
        List<String> names = new ArrayList<String>();
        
        String path = this.getClass().getResource("/").getPath();
        
        for(int i=0; i<resourceDirectory.length; i++)
        {
            File file = new File(path+resourceDirectory[i]);
            
            if( file.exists() && file.isDirectory() )
            {
                File[] files = file.listFiles();
                List<String> list = new ArrayList<String>();
                
                for( int j=0; j<files.length; j++ )
                {
                    String name = files[j].getName();
                    
                    if( checkAndAdd("_zh_CN", resourceDirectory[i], name, list) )
                        continue;
                    if( checkAndAdd("_zh_TW", resourceDirectory[i], name, list) )
                        continue;
                    if( checkAndAdd("_en", resourceDirectory[i], name, list) )
                        continue;
                    if( checkAndAdd("_en_US", resourceDirectory[i], name, list) )
                        continue;
                    if( checkAndAdd("_fr_CA", resourceDirectory[i], name, list) )
                        continue;
                    if( checkAndAdd("_fr_FR", resourceDirectory[i], name, list) )
                        continue;
                    if( checkAndAdd("_fr", resourceDirectory[i], name, list) )
                        continue;
                    if( checkAndAdd("_de", resourceDirectory[i], name, list) )
                        continue;
                    if( checkAndAdd("_de_DE", resourceDirectory[i], name, list) )
                        continue;
                    if( checkAndAdd("_it", resourceDirectory[i], name, list) )
                        continue;
                    if( checkAndAdd("_it_IT", resourceDirectory[i], name, list) )
                        continue;
                    if( checkAndAdd("_ja_JP", resourceDirectory[i], name, list) )
                        continue;
                    if( checkAndAdd("_ja", resourceDirectory[i], name, list) )
                        continue;
                    if( checkAndAdd("_ko_KR", resourceDirectory[i], name, list) )
                        continue;
                    if( checkAndAdd("_ko", resourceDirectory[i], name, list) )
                        continue;
                    if( checkAndAdd("_zh", resourceDirectory[i], name, list) )
                        continue;
                    if( checkAndAdd("_en_CA", resourceDirectory[i], name, list) )
                        continue;
                    if( checkAndAdd("_en_GB", resourceDirectory[i], name, list) )
                        continue;
                    if( checkAndAdd("", resourceDirectory[i], name, list) )
                        continue;
                }
                
                if(list.size()>0)
                    names.addAll(list);
            }
        }
        
        if(names.size()>0)
        {
            String[] beannames = new String[names.size()];
            for(int i=0; i<names.size(); i++)
                beannames[i] = names.get(i);
            
            this.setBasenames(beannames);
        }
    }
    
    private boolean checkAndAdd(String language, String directory, String name, List<String> list)
    {
        /*
        fr_CA
        fr_FR
        fr
        de
        de_DE
        it
        it_IT
        ja_JP
        ja
        ko_KR
        ko
        zh
        zh_CN
        zh_TW
        en
        en_CA
        en_GB
        en_US
         */
        language = language + ".properties";
        
        if( name.indexOf(language, name.length()-language.length())!=-1 )
        {
            String newName = name.replaceAll(language, "");

            boolean isAdd = true;
            for(int i=0; i<list.size(); i++)
            {
                if(list.get(i).equals(directory + "/" + newName))
                {
                    isAdd = false;
                    break;
                }
            }
            
            if(isAdd)
                list.add(directory + "/" + newName);
            
            return true;
        } 
        
        return false;
    }
}
